package com.superm.service.impl;

import com.superm.entity.Emp;
import com.superm.entity.RelationShip;
import com.superm.entity.RelationShipPoJo;
import com.superm.entity.RelationShipType;
import com.superm.mappers.RelationShipMapper;
import com.superm.service.RelationShipService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class RelationShipServiceImpl implements RelationShipService {

    @Autowired
    private RelationShipMapper relationShipMp;
    /**
     * 实现商品分类查询
     * @param relationShipPoJo
     * @return
     */
    @Override
    public List<RelationShip> selectRelationShipLike(RelationShipPoJo relationShipPoJo,Integer pageNo,Integer pageSize){
        RelationShip relationShip = new RelationShip();
        BeanUtils.copyProperties(relationShipPoJo,relationShip);
        relationShip.setRelationShipType(new RelationShipType(relationShipPoJo.getRelationShipType(),null));
        return relationShipMp.selectInfoMation(relationShip,(pageNo-1)*pageSize,pageSize);
    }

    /**
     * 实现商品的总条数查询
     * @param relationShipPoJo
     * @return
     */
    @Override
    public int selectRelationShipCount(RelationShipPoJo relationShipPoJo) {
        RelationShip relationShip = new RelationShip();
        BeanUtils.copyProperties(relationShipPoJo,relationShip);
        relationShip.setRelationShipType(new RelationShipType(relationShipPoJo.getRelationShipType(),null));
        return relationShipMp.selectInfoMationCount(relationShip);
    }

    /**
     * 实现查询指定分类
     * @param relationShipId
     * @return
     */
    @Override
    public RelationShip selectIdRelationShip(Integer relationShipId) {
        return relationShipMp.selectIdInfoMation(relationShipId);
    }

    /**
     * 查询所有最大类型
     * @return
     */
    @Override
    public List<RelationShip> selectPidMaxRelationShip(){
        return relationShipMp.selectMaxInfoMation();
    }

    /**
     * 修改商品分类
     * @param relationShipPoJo
     * @return
     */
    @Override
    public int updateRelationShip(RelationShipPoJo relationShipPoJo){
        RelationShip relationShip = new RelationShip();
        BeanUtils.copyProperties(relationShipPoJo,relationShip);
        relationShip.setRelationShipType(new RelationShipType(relationShipPoJo.getRelationShipType(),null));
        return relationShipMp.updateInfoMation(relationShip);
    }

    /**
     * 修改一级类型
     * @param relationShipPoJo
     * @return
     */
    @Override
    public int updateRelationShipOne(RelationShipPoJo relationShipPoJo) {
        RelationShip relationShip = new RelationShip();
        BeanUtils.copyProperties(relationShipPoJo,relationShip);
        relationShip.setRelationShipType(new RelationShipType(relationShipPoJo.getRelationShipType(),null));
        return relationShipMp.updateInfoMationOne(relationShip);
    }

    /**
     * 添加一级分类
     * @param relationShipName
     * @return
     */
    @Override
    public int addRelationShipOne(String relationShipName) {
        return relationShipMp.addOneInfoMation(relationShipName);
    }

    /**
     * 实现添加二级分类
     * @param relationShipPoJo
     * @return
     */
    @Override
    public int addRelationShipTow(RelationShipPoJo relationShipPoJo) {
        RelationShip relationShip = new RelationShip();
        BeanUtils.copyProperties(relationShipPoJo,relationShip);
        relationShip.setRelationShipType(new RelationShipType(relationShipPoJo.getRelationShipType(),null));
        return relationShipMp.addTowInfoMation(relationShip);
    }

    /**
     * 实现删除分类信息
     * @param relationShipId
     * @return
     */
    @Override
    public int deleteRelationShip(Integer relationShipId) {
        //查询指定编号底下是不是有子
        if (relationShipMp.selectPidCount(relationShipId)>0){
            return  -1;
        }else{
            //删除
            return relationShipMp.deleteInfoMation(relationShipId);
        }
    }

    /**
     * 实现查询所有子
     * @param pid
     * @return
     */
    @Override
    public List<RelationShip> selectPidList(Integer pid) {
        return relationShipMp.selectPidList(pid);
    }


}
